import pygame
from contants import *
import random

class Snake:
    def __init__(self):
        self.direction = pygame.K_RIGHT
        self.snake_body = [
            pygame.Rect(5*block_size,3*block_size,block_size,block_size),
            pygame.Rect(4*block_size,3*block_size,block_size,block_size),
            pygame.Rect(3*block_size,3*block_size,block_size,block_size),
            pygame.Rect(2*block_size,3*block_size,block_size,block_size)
        ]
        head_image = pygame.image.load("res/ikun.gif")
        self.snake_head_image = pygame.transform.scale(head_image,(block_size,block_size))
        body_image = pygame.image.load("res/ikun1.gif")
        self.snake_body_image = pygame.transform.scale(body_image,(block_size,block_size))
    
    def draw(self,screen):
        #蛇身体
        for node in self.snake_body[1:]:
            """pygame.draw.rect(screen,color_whilt,node,border_radius=50)"""
            body_image = pygame.transform.rotate(self.snake_body_image,head_dict[self.direction])
            screen.blit(body_image,(node.x,node.y))
        #绘制蛇头
        head: pygame.Rect = self.snake_body[0]
        head_image = pygame.transform.rotate(self.snake_head_image,head_dict[self.direction])
        screen.blit(head_image,(head.x,head.y))
        
            
    def update_direction(self,new_dir):
        #不能原地掉头
        LR = (pygame.K_LEFT,pygame.K_RIGHT)
        if self.direction in LR and new_dir in LR:
            return
        ud = (pygame.K_UP,pygame.K_DOWN)
        if self.direction in ud and new_dir in ud:
            return
        #符合条件，更新方向
        self.direction = new_dir
        
    """def is_direction_enable(self,)"""
            
    def move(self):
        #蛇头复制一份
        new_node = self.snake_body[0].copy()
        #王前进方向移动一格
        new_move = DIRECTION_DICT[self.direction]
        new_node.x += new_move[0]
        new_node.y += new_move[1]
        #穿墙
        if new_node.x>=screen_width:
            new_node.x-=screen_width
        if new_node.x<0:
            new_node.x+=screen_width
        if new_node.y>=screen_height:
            new_node.x-=screen_height
        if new_node.y<0:
            new_node.y+=screen_height
        #新蛇头放前面
        self.snake_body.insert(0,new_node)
        #吧蛇尾移除
        self.snake_body.pop() #索引不传参，默认删除最后一个元素
        
    def grow(self):
        #蛇尾复制一份
        new_node = self.snake_body[-1].copy()
        print(new_node)
        #添加新的节点到尾部
        self.snake_body.append(new_node)
        

            
class Food:
    def __init__(self,node):
        self.node = node
        food_image = pygame.image.load("res/da.png")
        self.food_image = pygame.transform.scale(food_image,(block_size,block_size))
        
        
    def draw(self,screen):
        """pygame.draw.rect(screen,color_blue,self.node)"""
        screen.blit(self.food_image,(self.node.x,self.node.y))
    
    #注解，静态函数，可直接用类名调用(类名调用，类名调用，类名调用)
    @staticmethod  
    def gen_foof_position(snake:Snake):
        """根据屏幕大小，蛇身生成新的食物

        Args:
            snake (Snake): 蛇对象
        """
        while True:
            x = random.randint(0,screen_width//block_size-10)
            y = random.randint(0,screen_height//block_size-10)
            new_food_node = pygame.Rect(x*block_size,y*block_size,block_size,block_size)
            if new_food_node not in snake.snake_body:
                return new_food_node